package com.leoman.plz_p.presenter;


import com.leoman.plz_p.interfaces.StudyRecordView;
import com.leoman.plz_p.module.api.CourseApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.BaseDataWithList;
import com.leoman.plz_p.module.bean.MissionBean;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.module.bean.StudyRecordBean;
import com.leoman.plz_p.subscriber.ResponseSubscriber;
import com.leoman.plz_p.utils.UserHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class StudyRecordPresenter extends BasePresenter<StudyRecordView> {

    private CourseApi courseApi;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        courseApi=getApi(CourseApi.class);
    }

    public void getStudyRecord(int page,String date){
        getView().showLoading();
        courseApi.getStudyRecord(UserHelper.getToken(),date,page+"",6+"")
                .compose(new ResponseTransformer<>(this.<BaseDataWithList<StudyRecordBean>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseDataWithList<StudyRecordBean>>(view) {
                    @Override
                    public void success(BaseDataWithList<StudyRecordBean> studyRecordBeanBaseDataWithList) {
                        view.obtainStudyRecord(studyRecordBeanBaseDataWithList.data);
                    }
                });
    }

    public void getMissionRecord(int page){
        getView().showLoading();
        courseApi.getMissionRecord(UserHelper.getToken(),page+"",6+"")
                .compose(new ResponseTransformer<>(this.<BaseData<PageListData<MissionBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<MissionBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<MissionBean>> missionBeanPageListData) {
                        view.obtainMissionRecord(missionBeanPageListData.data.getRows());
                    }
                });
    }

    @Override
    public void onStart() {
        getStudyRecord(1,new SimpleDateFormat("yyyy-MM").format(new Date()));
        getMissionRecord(1);
    }
}